package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.eduservice.service.feign.VodClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author lhl
 * @since 2022-07-04
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    VodClient vodClient;
    @Override
    public void removeViById(String id) {
        QueryWrapper<EduVideo> videoDeleteWrapper = new QueryWrapper<>();
        videoDeleteWrapper.eq("course_id",id);
        // 查询指定列
        videoDeleteWrapper.select("video_source_id");
        List<EduVideo> eduVideos = baseMapper.selectList(videoDeleteWrapper);
        //得到所有视频列表的云端原始视频id
        List<String> videoSourceIdList = new ArrayList<>();
        // 遍历eduVideos
        eduVideos.forEach((item)->{
            String videoSourceId = item.getVideoSourceId();
            // 如果id不为空就添加入list
            if(!StringUtils.isEmpty(videoSourceId)){
                videoSourceIdList.add(videoSourceId);
            }
        });
        if(videoSourceIdList.size()>0){
            vodClient.removeVideoList(videoSourceIdList);
        }
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_id",id);
        this.remove(eduVideoQueryWrapper);
    }
}
